package com.vm.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonArrayRequest;
import com.vm.adapter.IntergralAdapter;
import com.vm.commons.CurrentUser;
import com.vm.commons.VMConst;
import com.vm.model.Soft;
import com.vm.utils.JsonUtils;
import com.vm.utils.MyVolley;
import com.wwtscapp.apk.R;

public class HasGetPointFragment extends BaseFragment {
	protected static final String TAG = null;
	private RequestQueue mQueue;
	private List<Soft> listsofts;
	ListView lv_hasgetpoint;
	private View view;
	/** 标志位，标志已经初始化完成 */
	private boolean isPrepared;
	/** 是否已被加载过一次，第二次就不再去请求数据了 */
	private boolean mHasLoadedOnce;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		if (view == null) {
			view = inflater.inflate(R.layout.get_points_have_getted, null);
		} else {
			// 缓存的rootView需要判断是否已经被加过parent
			// 如果有parent需要从parent删除
			ViewGroup parent = (ViewGroup) view.getParent();
			if (parent != null) {
				parent.removeView(view);
			}
		}
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Log.e(TAG, "App");

		lv_hasgetpoint = (ListView) view.findViewById(R.id.lv_hasgetpoint);

		isPrepared = true;
		lazyLoad();

	}

	@Override
	protected void lazyLoad() {
		// TODO Auto-generated method stub

		if (!isPrepared || !isVisible || mHasLoadedOnce) {
			return;
		}

		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				// 显示加载进度对话框
				UIHelper.showDialogForLoading(getActivity(), "正在加载...", true);
			}

			@Override
			protected Boolean doInBackground(Void... params) {
				try {
					Thread.sleep(300);
					initData();
					// 在这里添加调用接口获取数据的代码
					// doSomething()
				} catch (Exception e) {
					e.printStackTrace();
				}
				return true;
			}

			@Override
			protected void onPostExecute(Boolean isSuccess) {
				if (isSuccess) {
					// 加载成功
					// setView();
					mHasLoadedOnce = true;
				} else {
					// 加载失败
				}
				// 关闭对话框
				UIHelper.hideDialogForLoading();
			}
		}.execute();

	}

	public void initData() {
		// llayout_list = (LinearLayout) view.findViewById(R.id.llayout_list);
		mQueue = MyVolley.getRequestQueue();
		listsofts = new ArrayList<Soft>();
		String url = VMConst.REQUEST_URL + "getHasGetIntergral?userId="
				+ CurrentUser.USERID;
		JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(url,
				new Listener<JSONArray>() {

					@Override
					public void onResponse(JSONArray arg0) {
						// TODO Auto-generated method stub
						for (int i = 0; i < arg0.length(); i++) {
							try {
								JSONObject jsonObject = arg0.getJSONObject(i);
								// String s=jsonObject.toString();

								Soft soft = JsonUtils.jsonToBean(jsonObject,
										Soft.class);

								listsofts.add(soft);

							} catch (JSONException e) {
								e.printStackTrace();
							}
						}

						IntergralAdapter adapter = new IntergralAdapter(
								getActivity(), listsofts);
						
						lv_hasgetpoint.setAdapter(adapter);

					}

				}, new Response.ErrorListener() {
					public void onErrorResponse(VolleyError error) {
						Log.e(TAG, error.getMessage(), error);
					}
				});
		mQueue.add(jsonObjectRequest);
	}

}
